package ui.prototypes;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DateSelector extends RelativeLayout {

    private ImageButton mLeftButton;

    private ImageButton mRightButton;

    private TextView mText;

    private OnValueChangeListener mOnValueChangeListener;

    private int mCurrentSelection = 0;

    private String[] monthStr;

    public DateSelector(Context context) {
        super(context);
        init();
    }

    public DateSelector(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public DateSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    protected void init() {

        monthStr = getContext().getResources().getStringArray(R.array.strMonths);

        mLeftButton = new ImageButton(getContext());
        mLeftButton.setImageResource(R.drawable.leftbutton);
        mLeftButton.setBackgroundDrawable(null);
        mLeftButton.setPadding(10, 10, 10, 10);
        mLeftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCurrentSelection > 0) {
                    setSelectedMonth(mCurrentSelection-1);
                }
            }
        });

        mRightButton = new ImageButton(getContext());
        mRightButton.setImageResource(R.drawable.rightbutton);
        mRightButton.setBackgroundDrawable(null);
        mRightButton.setPadding(10, 10, 10, 10);

        mRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCurrentSelection < 11) {
                    setSelectedMonth(mCurrentSelection+1);
                }
            }
        });

        mText = new TextView(getContext());
        //TODO styles must be included in xml files
        mText.setTextColor(getContext().getResources().getColor(R.color.bar_text));

        LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp1.addRule(ALIGN_PARENT_LEFT);
        addView(mLeftButton, lp1);

        LayoutParams lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp2.addRule(CENTER_IN_PARENT);
        addView(mText, lp2);

        LayoutParams lp3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp3.addRule(ALIGN_PARENT_RIGHT);
        addView(mRightButton, lp3);


        setSelectedMonth(0);
    }

    public void setOnValueChangeListener(OnValueChangeListener listener) {
        mOnValueChangeListener = listener;
    }

    public void setSelectedMonth(int val) {

        int oldValue = mCurrentSelection;

        mCurrentSelection = val;

        mText.setText(monthStr[mCurrentSelection]);

        if(oldValue != mCurrentSelection && mOnValueChangeListener != null) {
            mOnValueChangeListener.onValueChange(null, oldValue, mCurrentSelection);
        }

    }

}
